package cc.rengu.redp.bizimpl.igas.service.impl;

import cc.rengu.redp.bizimpl.igas.dao.SignAcctInfoMapper;
import cc.rengu.redp.bizimpl.igas.domain.SignAcctInfo;
import cc.rengu.redp.bizimpl.igas.service.SignAcctInfoService;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpSysParamService;
import cc.rengu.redp.bizimpl.upmp.vo.UpmpUserInfoVO;
import cc.rengu.redp.bizimpl.utils.CommonUtils;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.utils.SortUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

import static cc.rengu.redp.bizimpl.utils.DateUtils.formatDate;

@Service
@DS("igas")
public class SignAcctInfoServiceImpl extends ServiceImpl<SignAcctInfoMapper,SignAcctInfo> implements SignAcctInfoService {

    @Autowired
    private IUpmpSysParamService upmpSysParamService;

    @Autowired
    private SignAcctInfoMapper signAcctInfoMapper;

    @Override
    public IPage<SignAcctInfo> findPageQuery(QueryRequest request, SignAcctInfo info) {
        try {
            LambdaQueryWrapper<SignAcctInfo> queryWrapper = new LambdaQueryWrapper<>();
            UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
            String instId = "";
            if (user.isAdmin()) {
                instId = info.getInstId();
                if (StringUtils.isBlank(instId)) {
                    user.setInstId(instId);
                }
            }
            queryWrapper.likeRight(SignAcctInfo::getInstId, instId);
            if (StringUtils.isNotBlank(info.getInstId())) {
                queryWrapper.eq(SignAcctInfo::getInstId, info.getInstId());
            }
            if (StringUtils.isNotBlank(info.getCertType())) {
                queryWrapper.eq(SignAcctInfo::getCertType, info.getCertType());
            }
            if (StringUtils.isNotBlank(info.getCertNo())){
                queryWrapper.eq(SignAcctInfo::getCertNo, info.getCertNo());
            }
            if (StringUtils.isNotBlank(info.getSignAcctNo())) {
                queryWrapper.eq(SignAcctInfo::getSignAcctNo, info.getSignAcctNo());
            }
            Page<SignAcctInfo> page = new Page<>();
            SortUtil.handlePageSort(request, page, true);
            return this.page(page,queryWrapper);
        } catch (Exception e) {
            log.error("查询失败", e);
            return null;
        }
    }

    @Override
    public void rescind(QueryRequest request , SignAcctInfo info) {
        String updateTime = formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS");
        info.setUpdateTime(updateTime);
        String signStatus = "N";
        info.setSignStatus(signStatus);
        try {
            signAcctInfoMapper.rescind(info);
        } catch (Exception e) {
            log.error("解约失败", e);
        }
    }
}
